using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;

namespace JobsTutorialSample.Step3
{
    [BurstCompile]
    public struct FindNearestJobParallel : IJobParallelFor
    {
        [ReadOnly] public NativeArray<float3> SeekerPositions; //只读
        [ReadOnly] public NativeArray<float3> TargetPositions; //只读

        public NativeArray<float3> NearestTargetPositions; //可读可写

        public void Execute(int index)
        {
            float3 seekerPos = SeekerPositions[index];
            float3 pos = default;
            float nearestDisSq = float.MaxValue;
            foreach (var targetPos in TargetPositions)
            {
                float disSq = math.distancesq(seekerPos, targetPos);
                if (disSq < nearestDisSq)
                {
                    nearestDisSq = disSq;
                    pos = targetPos;
                }
            }

            NearestTargetPositions[index] = pos;
        }
    }
}